package com.example.officialdocumentsys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.officialdocumentsys.annotation.LogAnno;
import com.example.officialdocumentsys.common.R;
import com.example.officialdocumentsys.common.ThreadLocalContext;
import com.example.officialdocumentsys.po.Category;
import com.example.officialdocumentsys.po.Filecategory;
import com.example.officialdocumentsys.po.FileCategoryList;
import com.example.officialdocumentsys.service.FilecategoryService;
import com.example.officialdocumentsys.service.FilecategorylistService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hi
 * @since 2022-11-24
 */
@RestController
@RequestMapping("/file-category")
public class FileCategoryController extends BaseController<Filecategory, FilecategoryService>{
    //列出全部
    @GetMapping("/listAll")
    public R<List<Filecategory>> listAll(){
        LambdaQueryWrapper<Filecategory> qw = new LambdaQueryWrapper<>();
        return R.success(baseService.list(qw));
    }
    //展示用户自己
    @GetMapping("/listSelf")
    public R<List<Filecategory>> listSelf(){
        LambdaQueryWrapper<Filecategory> qw = new LambdaQueryWrapper<>();
        qw.eq(Filecategory::getUserId,ThreadLocalContext.getCurrentId());
        return R.success(baseService.list(qw));
    }
    //保存
    @LogAnno(operateType = "新增归档分类")
    @PostMapping
    public R<String> save(@RequestBody Filecategory e) {
        if (e.getName() == null){
            return R.error("分类名不可为空");
        }
        if (ThreadLocalContext.getCurrentId() == null){
            return R.error("未登入");
        }
        e.setUserId(ThreadLocalContext.getCurrentId());
        QueryWrapper<Filecategory> qw = new QueryWrapper<>();
        qw.lambda().eq(Filecategory::getName,e.getName());
        int count = baseService.count(qw);
        if (count > 0){
            return R.error("分类名已存在");
        }
        baseService.save(e);
        return R.success("保存成功");
    }

}

